﻿using System;
using ETModel;
using UnityEngine;

namespace ETHotfix
{
    public static class UIShowInfoFactory {
        /// <summary>
        /// 创建显示信息面板的UI(可以单独使用,也可以嵌入到其他UI里作为子UI使用)
        /// </summary>
        /// <returns></returns>
        private static UI CreateUI()
        {
            var abName = UIType.UIShowInfo.StringToAB();
            try
	        {
				ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
		        resourcesComponent.LoadBundle(abName);
				GameObject prefabGameObject = (GameObject)resourcesComponent.GetAsset(abName, UIType.UIShowInfo);
				GameObject gameObject = UnityEngine.Object.Instantiate(prefabGameObject);
		        UI ui = ComponentFactory.Create<UI, string, GameObject>(UIType.UIShowInfo, gameObject, false);

				ui.AddComponent<UIShowInfoComponent>();
				return ui;
	        }
	        catch (Exception e)
	        {
				Log.Error(e);
		        return null;
	        }
		}


        /// <summary>
        /// 将本UI加载到场景中
        /// </summary>
        public static void LoadUI() {
            UI ui = CreateUI();
            Game.Scene.GetComponent<UIComponent>().Add(ui);
        }
        /// <summary>
        /// 将本UI从场景中卸载
        /// </summary>
        public static void UnloadUI() {
            Game.Scene.GetComponent<UIComponent>().Remove(UIType.UIShowInfo);
            ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle(UIType.UIShowInfo.StringToAB());
        }
    }
}